<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
}  
include '../../operations/connection.php';

$a= (string)(microtime(true)*1000);
$b=split('\.',$a);
$partnerID= "UC".$b[0];

$parnername = $_REQUEST['parnername'];
$password = strtoupper(md5($_REQUEST['password']));
$repassword = $_REQUEST['repassword'];
$databasename = $_REQUEST['databasename'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$website = $_REQUEST['website'];
$domainname = $_REQUEST['domainname'].".erp.elisoft.co.kr";
$roothome = $_REQUEST['roothome'];
$status = $_REQUEST["status"];

//$keynametemp = explode(".",$domainname);
if ($_REQUEST['domainname']==""){
	$keyname = "erp";
}else{
	$keyname = $_REQUEST['domainname'];
}


$idpartnercomp = $_REQUEST['idpartnercomp'];

if(isset($_REQUEST['register'])){
	if(isset($_SESSION['checkdb_domain']) && $_SESSION['checkdb_domain']=="okk"){
		$insertpartner = "insert into ac_eli_manager(PartnerID,PartnerName,Email,Phone,Website,UserName,Password,DatabaseName,Role,Status,DomainName,KeyName)
		VALUES('$partnerID','$parnername','$email','$phone','$website','admin','$password','$databasename',2,2,'$domainname','$keyname');";
	mysql_query($insertpartner);

	set_time_limit( 0 );

	$filename = '../../database.sql';
	//$mysql_host = '1.234.53.52';
	//$mysql_username = 'cam';
	//$mysql_password = '5678';
	$mysql_database = $databasename;

	//mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
	mysql_query("CREATE DATABASE $mysql_database DEFAULT CHARACTER SET  'utf8' DEFAULT COLLATE 'utf8_general_ci';");
	mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

	$templine = '';
	$lines = file($filename);
	foreach ($lines as $line_num => $line) {
		if (substr($line, 0, 2) != '--' && $line != '') {
			$templine .= $line;
			if (substr(trim($line), -1, 1) == ';') {
				mysql_query($templine) or print('Error performing query \'<b>' . $templine . '</b>\': ' . mysql_error() . '');
				$templine = '';
			}
		}
	}

	$p="
	CREATE   PROCEDURE getTotalDocument(IN idemployee varchar(15))
	BEGIN
		select (SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as RePendingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 2 )  as ReNotApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 1 AND CONVERT(b.IDLabel USING UTF8) < 1
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as ReApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 3 AND CONVERT(b.IDLabel USING UTF8) < 1
			AND CONVERT(b.IDStatus USING UTF8) = 3 )  as ReDeclinedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc p INNER JOIN list_reference l ON
			CONVERT(p.IDGroupApprove USING UTF8) = CONVERT(l.id_group USING UTF8)
			WHERE CONVERT(l.id_employee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(p.IDTypeDoc USING UTF8)   != 11 AND CONVERT(l.IDLabel USING UTF8) < 1
			AND CONVERT(l.id_status USING UTF8)= 1)  as ReRefApprovedDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc p INNER JOIN list_reference l ON
			CONVERT(p.IDGroupApprove USING UTF8) = CONVERT(l.id_group USING UTF8)
			WHERE CONVERT(l.id_employee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(p.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(l.id_status USING UTF8)= 2)  as ReRefWaitingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8) = 11
			AND CONVERT(a.IDStatus USING UTF8) = 2
			AND CONVERT(b.IDStatus USING UTF8) = 2 )  as ReMeetingDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 2
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 ) as MySendingDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 4
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MySavedDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 1 AND CONVERT(a.IDLabel USING UTF8) < 1
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyApproveDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 3 AND CONVERT(a.IDLabel USING UTF8) < 1
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyDeclinedDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 2
			AND CONVERT(a.IDTypeDoc USING UTF8) = 11
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as MyMeetingDoc,
	
		(SELECT count(IDTypeDoc)  FROM approvedoc a INNER JOIN listapprove b
			ON a.IDGroupApprove = b.IDGroupApprove
			WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)
			AND CONVERT(a.IDTypeDoc USING UTF8)   != 11
			AND CONVERT(a.IDStatus USING UTF8) = 6
			AND CONVERT(b.IDStatus USING UTF8) = 1 )  as BinReceiverDoc,
	
		(SELECT count(IDTypeDoc) from approvedoc a
			WHERE CONVERT(a.IDStatus USING utf8) = 6
			AND CONVERT(a.IDEmployee USING UTF8) = CONVERT(idemployee  USING UTF8)) as BinSendingDoc
		;
	END;
	";
	
	mysql_query($p);
	
	
	mysql_query("INSERT INTO employee(IDEmployee,UserName,Password, 
			FullName,IDManager,IDRole,Email,Address,Position,
			Mobile,State) VALUES(1,'admin','$password','admin',1,1,'$email','',1,'$phone',1);");
	}

}else if (isset($_REQUEST['update'])){
	$updatepartner = "UPDATE ac_eli_manager set PartnerName='$parnername',Email='$email',Phone='$phone',Website='$website',
		Password='$password',DatabaseName='$databasename',RootHome='$roothome',`Status`='$status' where  PartnerID='$idpartnercomp'";
	mysql_query($updatepartner);
}
mysql_close($connection);


if(isset($_REQUEST['register'])){
	$_SESSION['insertpartner'] ="";
	header("Location: ../../partnermanage.php");
}
if (isset($_REQUEST['update'])){
	$_SESSION['updatepartner'] ="";
	header("Location: ../../partnermanage.php");
}

ob_flush();


?>